// pages/mine/collect/collect.js
const app = getApp()
const util = require("../../../../utils/util.js");
var page = 1;
import tools from "../../../../utils/tools.js";
var load = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectList: [],
        showCheckbox: false,
        waitNum: 0,
        showCot: false,
        chouseMessaages: [],
        type: "",
        showCot: false,
        type_id: "",
        onload: true,
        ids:[],
        mids:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            type: options.type,
            type_id: options.id,
        });
   
    },

    onShow() {
        this.render()
    },
    render() {
        load = true;
        this.setData({
            onload: true,
        });
        tools.getUserCollect("").then(res => {
            this.setData({ collectList: res, showCheckbox: false, showCot: false, onload: false });
        });
    },
    // 删除收藏
    delCollect(e) {
        ks.showModal({
            title: '删除选中的内容',
            content: '此操作会同步删除其他收藏夹选中的内容',
            success: res => {
                if (res.confirm)
                    tools.deleteCollect(`${this.data.ids.join(",")}`).then(res => {
                        this.render();
                    });
            }
        });
    },

    done() {
        this.setData({
            showCheckbox: false,
        })
    },

    chouseMessage(e) {
        let ids = []
        let mids = []
        e.detail.value.forEach(val =>{
            let obj = JSON.parse(val)
            ids.push(obj.value.id)
            mids.push(obj.id)
        })
        this.setData({
            ids,
            mids
        });
    },

    // 跳转
    navBtn(e) {
        let index = e.currentTarget.dataset.index;
        let data = this.data.collectList[index].value;
        let id = data.id;
        let nav = data.nav;
        let citycode = this.data.collectList[index].citycode;
        let path = data.path;
        if (path == "/pages/topicality/bsy/bsy") {
            ks.navigateTo({
                url: path + '?id=' + id + '&city=' + citycode,
            })
        } else {
            ks.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + nav + '&city=' + citycode,
            })
        }

    },

    more() {
        this.setData({
            showCot: true,
        })
    },

    cancel() {
        this.setData({
            showCot: false,
        })
    },

    editAll() {
        this.setData({
            showCheckbox: true,
        })
    },

    moveTo(e) {
        tools.moveCollect(9, this.data.mids.join(","), ).then(res => ks.showToast({
            title: '移入代办成功',
            icon: 'none',
            success: res => {
                this.closemove();
                this.render();
            }
        }));
    },

    closemove() {
        this.setData({
            showCot: false,
        })
    },

    top() {
        tools.top(this.data.mids.join(",")).then(res => {
            this.closemove();
            this.render();
        });
    },
    onReachBottom: function() {
        page++;
        if (load)
            tools.getUserCollect("", page).then(res => {
                if (res[0]) {
                    res.forEach(item => this.data.collectList.push(item));
                    this.setData({
                        collectList: this.data.collectList,
                    });
                } else
                    load = false;
            });
    },

    // 添加新消息到新建收藏夹 
    add() {
        tools.moveCollect(this.data.type_id, this.data.mids.join(","), ).then(res => {
            ks.navigateBack({
                delta: 1
            });
        })
    },

    onShareAppMessage: function() {

    }
})